package com.meihua.shellapp.fragment;


import android.os.Bundle;
import android.support.v7.widget.RecyclerView;

import com.aspsine.irecyclerview.IRecyclerView;
import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.adapter.InsuranceTypeRVAdapter;
import com.meihua.shellapp.base.BaseRecyclerViewFragment1;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.ProductIndexBean;
import com.meihua.shellapp.bean.ZhongAnBean;
import com.meihua.shellapp.common.Constant;
import com.meihua.shellapp.common.NetHelper;
import com.meihua.shellapp.utils.URL;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;

/**
 * 产品中心 保险分类界面
 */
public class InsuranceTypeFragment extends BaseRecyclerViewFragment1 {
    @Bind(R.id.ins_iRecyclerView)
    IRecyclerView mRecyclerView;

    private ArrayList<ProductIndexBean.DataBean.ProductBeanX> mData;//产品
    private ArrayList<ProductIndexBean.DataBean.TypeIdBean> mImgData;//照片
    private InsuranceTypeRVAdapter rvAdapter;
    private String id;//id
    private List<ZhongAnBean.DataBean> mDataZhAn;//众安产品

    /*布局ID*/
    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_insurancetype;
    }

    @Override
    protected void initView() {
        /*获取传递过来的ID*/
        id = getArguments().getString(Constant.FRAGMENT_URL);
        initRecyclerView(mRecyclerView);

    }

    /*设置adapter*/
    @Override
    protected RecyclerView.Adapter getAdapter() {
        mData = new ArrayList<>();
        mImgData = new ArrayList<>();
        mDataZhAn = new ArrayList<>();
        rvAdapter = new InsuranceTypeRVAdapter(mData, mImgData, getActivity());
        return rvAdapter;
    }

    /*请求服务器数据*/
    @Override
    public void getServiceData(int page) {
        showLoadingDialog();
        Map<String, String> params = new HashMap() {{
            put("type", id);
        }};
        NetHelper.post(URL.getUrl().PRODUCT_INDEX, params, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(Response<String> response) {
                dismissLoadingDialog();
                if (mRecyclerView == null) return;
                mRecyclerView.setRefreshing(false);
                ProductIndexBean bean = MyApplication.getInstance().getBeanForJson(ProductIndexBean.class, response.body());
                if (bean == null) return;
                mData.clear();
                mImgData.clear();
                mData.addAll(bean.getData().getProduct());
                mImgData.addAll(bean.getData().getType_id());
                rvAdapter.notifyDataSetChanged();
            }
        }, true);

    }


    /*initEN*/
    public static InsuranceTypeFragment newInstance(String id) {
        InsuranceTypeFragment mInstance = new InsuranceTypeFragment();
        Bundle args = new Bundle();
        args.putString(Constant.FRAGMENT_URL, id);
        mInstance.setArguments(args);
        return mInstance;
    }
}